package main

import (
	"fmt"
	_ "gowork/studentGoKit/model"
	"gowork/studentGoKit/server/util"
	"log"
	"net/http"
	"github.com/gorilla/mux"
	"gowork/studentGoKit/server/handler"
	"os"
	"os/signal"
	"syscall"
)

func main(){
	router := mux.NewRouter()

	router.Methods("GET").Path("/insert").Handler(handler.StudentInsert())
	router.Methods("GET").Path("/delete").Handler(handler.StudentDelete())
	router.Methods("GET").Path("/update").Handler(handler.StudentUpdate())
	router.Methods("GET").Path("/query").Handler(handler.StudentQuery())

	router.Methods("GET").Path("/health").HandlerFunc(func(w http.ResponseWriter, r *http.Request){
		w.Header().Set("Content-type", "application/json")
		w.Write([]byte(`{"status":"ok"}`))
	})

	errChan := make(chan error)
	go (func(){
		util.RegSerrvice()
		err := http.ListenAndServe(":7777", router)
		if err != nil{
			log.Fatal(err)
			errChan <- err
		}
	})()

	go (func(){
		sig_c := make(chan os.Signal)
		signal.Notify(sig_c, syscall.SIGINT, syscall.SIGTERM)
		errChan <- fmt.Errorf("%s", <-sig_c)
	})()

	gerErr := <- errChan
	util.UnRegSerrvice()
	log.Println(gerErr)
}